package com.vikutorika.servlet;

import cn.hutool.json.JSONUtil;
import com.vikutorika.vo.ResultVo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class NewBaseServlet extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String methodName = req.getParameter("methodName");
        System.out.println(methodName);
        try {
            Class<?> aClass = this.getClass();
            Method method = aClass.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            Object invoke = method.invoke(this, req, resp);
            resp.getWriter().write(JSONUtil.toJsonStr(invoke));
        } catch (Exception e) {
            resp.getWriter().write(JSONUtil.toJsonStr(ResultVo.error("错误请求")));
            throw new RuntimeException(e);
        }
    }
}
